<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags" %>

<fieldset class="form-container " style="width:80%;margin-left:5%;"> 

    <legend class="form-title"><h2>Recherche d'un contrat</h2></legend>
    <h3 style="margin-bottom: 5%;margin-left: 1%">Veuillez renseigner les champs ci-dessous :</h3>
    <div style="float: left;position: relative;">
        <p class="form-title">Rechercher un contrat
            <input type="text" id="rechercheAdherent" name="adherentRecherche" value="${adherentRecherche}"/>
            <button class="submit-button" id="rechercheAdherentButton" style="padding: 2.5px 8px;">Recherche</button></p>


    </div>
    <div style="clear: left">
        <form id="form_782213"    method="get" action="/bulletin/add.do"  autocomplete="on">
            <div id="resultAdhrent"></div>
            <div class="submit-container" style=" clear: left;">
                <button class="submit-button" type="submit" >Ajouter Bulletin</button>
                <button class="submit-button" id="annuler" type="reset">Annuler</button>
            </div>
        </form>
    </div>
</fieldset>





<script>
    $(function() {
        $('#dateEntree').datepicker({dateFormat: 'dd mm yy'})
        $("button[id=annuler]")
                .click(function(event) {
                    window.location = "/bulletin/index.do";
                });
        $("button[id=rechercheAdherentButton]")
                .click(function(event) {
                    event.preventDefault();
                    event.preventDefault();
                    $.ajax({
                        url: '/bulletin/searchContrat.do?field=' + $('#rechercheAdherent').val(),
                        type: "POST",
                        beforeSend: function(xhr) {

                            xhr.setRequestHeader("Accept", "application/json");

                            xhr.setRequestHeader("Content-Type", "application/json");

                        },
                        success: function(contrat) {
                            if (contrat.length == 0) {
                                var string = "Aucun résultat";
                            } else {
                                var string = "<div class=\"datagrid\"><table ><thead><tr><th>Adhérent</th><th>Employe</th><th>Sélectionner</th</tr></thead>";
                                for (i = 0; i < contrat.length; i++) {
                                    string += "<tr><td>" + contrat[i].adherent.nomPrenom + "</td><td>" + contrat[i].employe.nom + " " + contrat[i].employe.prenom + "</td><td><input type=\"radio\" name=\"idContrat\" value=" + contrat[i].id + "></td></tr>";
                                }




                                string += "</tbody></table></div>";
                            }
                            $('#resultAdhrent').html(string);


                        }

                    });
                });
    });
</script>